import React from 'react';
import { Checkbox } from 'antd';
import { isFunction } from '../../../utils/is-object';

export default function createCheckbox(config = {}) {
  const {
    property,
    data = {},
    size = 'small',
    onChange,
    getter = (data, property) => data[property],
    getValue = value => value
  } = config;
  const onChangeHandler = event => {
    let value = event.target.checked;
    if (isFunction(getValue)) {
      value = getValue(value);
    }
    if (isFunction(onChange)) {
      onChange(value, property);
    }
  };
  const value = getter(data, property);

  return <Checkbox checked={value} onChange={onChangeHandler} size={size} />;
}
